home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / gcutil.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  13KB  |  366 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import wx
  6. import random
  7. import gc
  8. import sys
  9. from weakref import ref
  10. from collections import defaultdict
  11. from inspect import currentframe
  12. from types import FrameType
  13. from traceback import print_exc
  14. from util import better_ref_types
  15. from logging import getLogger
  16. log = getLogger('gcutil')
  17. import locale
  18. enc = locale.getpreferredencoding()
  19. SET_AS_ROOT = wx.NewId()
  20. SET_SHELL_OBJ = wx.NewId()
  21. NEW_GCTREE = wx.NewId()
  22. DELETE = wx.NewId()
  23. custom_reprs = {
  24.     FrameType: (lambda f: '%r for %r' % (f, f.f_code)) }
  25. weak = (ref, better_ref_types)
  26.  
  27. def isweak(obj):
  28.     return isinstance(obj, weak)
  29.  
  30.  
  31. class gctree_ref(ref):
  32.     
  33.     def __init__(self, obj, cb = None):
  34.         ref.__init__(self, obj, cb)
  35.  
  36.  
  37.  
  38. def deref(wr):
  39.     return None if isinstance(wr, gctree_ref) else wr
  40.  
  41.  
  42. def repritem(i):
  43.     funcinfo = funcinfo
  44.     import util
  45.     
  46.     try:
  47.         if type(i) in custom_reprs:
  48.             return custom_reprs[type(i)](i)
  49.         
  50.         repr_txt = unicode(repr(i)).decode(enc, 'replace')
  51.         if repr_txt.startswith('<function <lambda> at'):
  52.             repr_txt = funcinfo(i)
  53.     except (UnicodeDecodeError, UnicodeEncodeError, wx.PyDeadObjectError):
  54.         return '<%s at %x>' % (type(i).__name__, id(i))
  55.  
  56.     return repr_txt
  57.  
  58.  
  59. def makeref(obj):
  60.     
  61.     try:
  62.         return gctree_ref(obj)
  63.     except TypeError:
  64.         e = None
  65.         if repr(e).find('cannot create weak reference to') != -1:
  66.             return obj
  67.         
  68.         raise 
  69.  
  70.  
  71.  
  72. class colors:
  73.     cycle = wx.Color(128, 128, 128)
  74.     weakref = wx.Color(100, 255, 100)
  75.     root = wx.Color(255, 0, 0)
  76.  
  77.  
  78. class LazyTree(wx.TreeCtrl):
  79.     
  80.     def __init__(self, parent, root = None):
  81.         super(LazyTree, self).__init__(parent)
  82.         Bind = self.Bind
  83.         Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnExpandItem)
  84.         Bind(wx.EVT_TREE_ITEM_COLLAPSING, self.OnCollapseItem)
  85.         Bind(wx.EVT_TREE_ITEM_MENU, self.OnTreeMenuItem)
  86.         Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
  87.         Bind(wx.EVT_MENU, self.OnMenu)
  88.         Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
  89.         self._LazyTree__collapsing = False
  90.         self.getChildren = gc.get_referrers
  91.         if root is not None:
  92.             self.setroot(root)
  93.         
  94.  
  95.     
  96.     def setfunc(self, func):
  97.         self.getChildren = func
  98.         self.setroot(deref(self.rootref))
  99.  
  100.     
  101.     def setroot(self, root):
  102.         self.DeleteAllItems()
  103.         rootId = self.AddRoot(repritem(root))
  104.         self.SetPyData(rootId, makeref(root))
  105.         self.SetItemHasChildren(rootId)
  106.         self.rootref = makeref(root)
  107.  
  108.     
  109.     def OnRightDown(self, e):
  110.         (item, area) = self.HitTest(e.Position)
  111.         self.SelectItem(item)
  112.         e.Skip()
  113.  
  114.     
  115.     def OnKeyDown(self, e):
  116.         if e.KeyCode == wx.WXK_DELETE:
  117.             self.Delete(self.GetSelection())
  118.         else:
  119.             e.Skip()
  120.  
  121.     
  122.     def OnExpandItem(self, event):
  123.         parentItem = event.Item
  124.         wr = self.GetPyData(parentItem)
  125.         parent = deref(wr)
  126.         if parent is None:
  127.             return None
  128.         
  129.         curframe = currentframe()
  130.         count = 0
  131.         for child in self.getChildren(parent):
  132.             childStr = repritem(child)
  133.             if child is parent:
  134.                 continue
  135.             
  136.             if 'gcutil' in childStr and 'OnExpandItem' in childStr:
  137.                 continue
  138.             
  139.             childItem = self.AppendItem(parentItem, childStr)
  140.             self.SetPyData(childItem, makeref(child))
  141.             if self.IsCycle(childItem):
  142.                 self.SetItemTextColour(childItem, colors.cycle)
  143.             elif isweak(child):
  144.                 self.SetItemTextColour(childItem, colors.weakref)
  145.             else:
  146.                 self.SetItemHasChildren(childItem, True)
  147.             count += 1
  148.         
  149.         if count == 1:
  150.             self.Expand(childItem)
  151.         elif count == 0:
  152.             self.SetItemHasChildren(parentItem, False)
  153.         
  154.  
  155.     
  156.     def IsCycle(self, item):
  157.         GetItemParent = self.GetItemParent
  158.         GetPyData = self.GetPyData
  159.         
  160.         def GetPyData(item, get_data = self.GetPyData):
  161.             return deref(get_data(item))
  162.  
  163.         obj = GetPyData(item)
  164.         while True:
  165.             item = GetItemParent(item)
  166.             if not item.Ok():
  167.                 break
  168.             
  169.             if GetPyData(item) is obj:
  170.                 return True
  171.                 continue
  172.         return False
  173.  
  174.     
  175.     def OnCollapseItem(self, event):
  176.         if self._LazyTree__collapsing:
  177.             return event.Veto()
  178.         
  179.         self._LazyTree__collapsing = True
  180.         item = event.Item
  181.         self.CollapseAndReset(item)
  182.         self.SetItemHasChildren(item)
  183.         self._LazyTree__collapsing = False
  184.  
  185.     
  186.     def OnTreeMenuItem(self, e):
  187.         m = wx.Menu()
  188.         m.Append(NEW_GCTREE, 'New GCTree with this object')
  189.         m.Append(SET_AS_ROOT, 'Set as Root')
  190.         if hasattr(wx.GetApp(), 'shell_locals'):
  191.             m.Append(SET_SHELL_OBJ, 'Set in Shell as "obj"')
  192.         
  193.         m.AppendSeparator()
  194.         m.Append(DELETE, 'Delete')
  195.         self.PopupMenu(m)
  196.  
  197.     
  198.     def OnMenu(self, e):
  199.         obj = self.GetPyData(self.GetSelection())
  200.         i = e.Id
  201.         if i == SET_AS_ROOT:
  202.             self.setroot(obj)
  203.         elif i == SET_SHELL_OBJ:
  204.             wx.GetApp().shell_locals.update(obj = obj)
  205.         elif i == NEW_GCTREE:
  206.             gctree(makeref(obj))
  207.         elif i == DELETE:
  208.             self.Delete(self.GetSelection())
  209.         
  210.  
  211.  
  212.  
  213. class GCInfoPanel(wx.Panel):
  214.     
  215.     def __init__(self, parent):
  216.         wx.Panel.__init__(self, parent)
  217.         self.Sizer = s = wx.FlexGridSizer(0, 2, 5, 5)
  218.         rows = [
  219.             ('__repr__', repritem),
  220.             ('Reference Count', sys.getrefcount),
  221.             ('referrers', (lambda obj: len(gc.get_referrers(obj))))]
  222.         if hasattr(sys, 'getsizeof'):
  223.             rows.extend([
  224.                 ('Size', sys.getsizeof)])
  225.         
  226.         self.ctrls = []
  227.         for label, func in rows:
  228.             s.Add(wx.StaticText(self, -1, label))
  229.             ctrl = wx.StaticText(self, -1, '')
  230.             s.Add(ctrl)
  231.             self.ctrls.append((ctrl, func))
  232.         
  233.  
  234.     
  235.     def set_object(self, obj):
  236.         for ctrl, func in self.ctrls:
  237.             
  238.             try:
  239.                 ctrl.Label = str(func(obj))
  240.             continue
  241.             except Exception:
  242.                 e = None
  243.                 ctrl.Label = str(e)
  244.                 continue
  245.             
  246.  
  247.         
  248.         self.Layout()
  249.  
  250.  
  251.  
  252. class GCTreeFrame(wx.Frame):
  253.     
  254.     def __init__(self, item):
  255.         size = wx.Size(1000, 600)
  256.         wx.Frame.__init__(self, None, name = 'GCTreeFrame', size = size)
  257.         split = wx.SplitterWindow(self, style = wx.SP_LIVE_UPDATE)
  258.         self.gcinfopanel = GCInfoPanel(split)
  259.         tree = LazyTree(split, item)
  260.         
  261.         def OnSelection(e):
  262.             tree_item = e.Item
  263.             if tree_item:
  264.                 wr = tree.GetPyData(tree_item)
  265.                 self.gcinfopanel.set_object(deref(wr))
  266.             
  267.  
  268.         tree.Bind(wx.EVT_TREE_SEL_CHANGED, OnSelection)
  269.         self.SetTitle(repritem(item))
  270.         split.SetSashGravity(1)
  271.         split.SetMinimumPaneSize(100)
  272.         split.SplitVertically(tree, self.gcinfopanel)
  273.         ((None, None), wx.CallAfter)((lambda : split.SetSashPosition(size.width - 250)))
  274.         panel = wx.Panel(self)
  275.         b1 = wx.RadioButton(panel, -1, 'refer&rers')
  276.         b2 = wx.RadioButton(panel, -1, 'referen&ts')
  277.         
  278.         def onradio(e, itemref = (None, makeref(item))):
  279.             func = None if e.EventObject is b1 else gc.get_referents
  280.             tree.setfunc(func)
  281.  
  282.         self.Bind(wx.EVT_RADIOBUTTON, onradio)
  283.         self.Bind((wx.EVT_CLOSE,), (lambda e: self.Destroy()))
  284.         b1.SetValue(True)
  285.         b1.SetToolTipString(gc.get_referrers.__doc__)
  286.         b2.SetToolTipString(gc.get_referents.__doc__)
  287.         s2 = panel.Sizer = wx.BoxSizer(wx.HORIZONTAL)
  288.         s2.AddMany([
  289.             (5, 10),
  290.             (b1, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL),
  291.             (10, 20),
  292.             (b2, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)])
  293.         s = wx.BoxSizer(wx.VERTICAL)
  294.         s.AddMany([
  295.             (split, 1, wx.EXPAND),
  296.             (panel, 0, wx.EXPAND)])
  297.         self.Sizer = s
  298.         s.Layout()
  299.  
  300.     
  301.     def onradio(self):
  302.         pass
  303.  
  304.  
  305.  
  306. def gctree(obj):
  307.     gc.collect()
  308.     GCTreeFrame(obj).Show()
  309.  
  310.  
  311. try:
  312.     import sip
  313. except ImportError:
  314.     pass
  315.  
  316.  
  317. def leaked():
  318.     gc.collect()
  319.     import util
  320.     return (lambda .0: for o in .0:
  321. if sip.isdeleted(o):
  322. ocontinue)(util.uncollectable(sip.wrapper))
  323.  
  324.  
  325. def count(identifier):
  326.     if isinstance(identifier, basestring):
  327.         objs = byclassname(identifier)
  328.     else:
  329.         objs = byclass(identifier)
  330.     return sum((lambda .0: for o in .0:
  331. 1)(objs))
  332.  
  333.  
  334. def byclass(cls):
  335.     gc.collect()
  336.     return (lambda .0: for o in .0:
  337. if isinstance(o, cls):
  338. ocontinue)(gc.get_objects())
  339.  
  340.  
  341. def byclassname(classname):
  342.     gc.collect()
  343.     return (lambda .0: for o in .0:
  344. if type(o).__name__ == classname:
  345. ocontinue)(gc.get_objects())
  346.  
  347.  
  348. def byaddress(addr):
  349.     objs = _[1]
  350.  
  351. if __name__ == '__main__':
  352.     from tests.testapp import testapp
  353.     import gui.native.helpers as gui
  354.     a = 5
  355.     b = [
  356.         'some items',
  357.         a]
  358.     c = [
  359.         b,
  360.         'some other items']
  361.     app = testapp('../../')
  362.     frame = GCTreeFrame(c)
  363.     frame.Show()
  364.     app.MainLoop()
  365.  
  366.